$('#addBirth').css('height',($(window).height())+'px');
$(".detail").css('height',($(window).height()-90)+'px');
$("#add").click(function () {
    console.log("add birth..")
    $("#addBirth").css('display','block');
    $(".addBirth-form").show();
});
//选择地区
Area.init("#area_province","#area_city","#area_area");
//选择测试人员
function selectBirth(id,index,total){
// console.log(index + "-" +total);
    $("#birthid").val(id);
    for(var i=0;i<total;i++){

        if(i==index){
            $("#birth"+i).removeClass();
            $("#birth"+i).addClass('births-item2-selected');
            // console.log("set selected "+index+"-"+i);
        }else{
            // console.log("set nonselected "+index+"-"+i);
            $("#birth"+i).removeClass();
            $("#birth"+i).addClass('births-item2');
        }

    }
};
//删除Birth
function delBirth(id){
    server.req('wechat/birth/delete/'+id,{},function(e){
        if(e.code==0){
            server.loadindex('page/teacher/teacherdetail/teacherdetail',window.location.search.substring(1))
        }else{
            pop_up(e.msg);
        }
    },null,"POST");
};
function savebirth(id){
    if(id==1){
        var obj=$("#quickaskdetail_frm_main").serializeObject();
        console.log(obj);
        if(server.isEmpty(obj.name)||server.isEmpty(obj.birthday)||server.isEmpty(obj.sex)||server.isEmpty(obj.province)||server.isEmpty(obj.city)||server.isEmpty(obj.area)){
            pop_up("信息输入不完整")
            return;
        }
        var birth = {};
        birth.name = obj.name;
        birth.birthday  = new Date(obj.birthday).format("yyyy-MM-dd hh:mm:ss");
        birth.birthCity = obj.city;
        birth.birthXian = obj.area;
        birth.birthProvince = obj.province;
        birth.birthCountry = "中国";
        birth.sex = obj.sex;

        console.log(birth)
        server.req('wechat/birth/save',birth,function (e) {
            console.log(e);
            if(e.code==0&&e.data){
                $("#addBirth").css('display','none');
                $(".addBirth-form").hide();
                server.loadindex('page/quickask/quickaskdetail/quickaskdetail',window.location.search.substring(1))
            }else{
                pop_up(e.msg);
            }
        },null,"POST")
    }else if(id==2){
        $("#addBirth").css('display','none');
        $(".addBirth-form").hide();
    }
};

//下单
function submitorder(){
    var  obj ;
    obj={};
    var serviceType= $("#serviceType").val();
    obj=$("#quickaskdetail_frm_main").serializeObject();


    // var teacherid = server.getQueryString("teachid");

    // obj.teacherId=teacherid;
    var item= {};
    obj.itemList=[];

    if(serviceType=="3") {
        item = {
            itemType: obj.serviceType,
            itemName: obj.description,
            description: obj.description,
            birthId: obj.birthid,
            openQuestions: (obj.publicask && obj.publicask == "on") ? 1 : 0
        };
    }else if(serviceType=="1")  {
        item = {
            itemType: obj.serviceType,
            itemId:obj.service

        };
    }else if(serviceType=="2")  {
        item = {
            itemType: obj.serviceType,
            itemId:obj.serviceId,
            birthId: obj.birthid
        };
    }
    //  itemList.push(item);
    obj.itemList.push(item);
    console.log("quick ask ojb:")
    console.log(obj)
    server.req("wechat/order/createOrder",obj,function (e){
        // console.log(e);
        if(e.code==0){
            server.setCache("orderInfo",JSON.stringify(e.data));
            server.loadindex('page/order/submitorder/submitorder',"",true);
        }else{
            pop_up(e.msg)
        }
    },null,"POST")

};

$(function () {
    var articleid = server.getQueryString('id');
    if(articleid){
        server.req('wechat/quicktest/info/'+articleid,{},function (res) {
            console.log(res);
            if(res.code==0&&res.data){
                // $("#serviceId").val(res.data.id);
                $("#serviceId").val(articleid);
                server.loadData(".detail",res.data);
                server.loadData(".menu_bar2",res.data);

                //增加浏览次数
                var opentimes = res.data.openTimes;
                opentimes=Number(opentimes)+1;
                var params={id:res.data.id,openTimes:opentimes};
                server.req('wechat/quicktest/update',params,function (res) {
// console.log(res)
                },null,"POST");
            }
        });
    };


    //加载当前用户的Birth列表
    server.req('wechat/birth/list',{},function (e) {
        var strBirthHtml="";
        if(e.code==0){
            var birthList = e.data;
            var birthNum = e.size;
            if(birthList[0]){
                // console.log("birthList="+birthList[0].id);
                // console.log(birthList[0])
                $("#birthid").val(birthList[0].id);
            }

            birthList.forEach(function(item,index){
                console.log("index:"+index)
                if(index==0){
                    strBirthHtml = strBirthHtml+ '<div class="births-item2-selected" id="birth'+index+'" onclick="selectBirth('+item.id+','+index+','+birthNum+')">\n' +
                        '                        <label>'+item.name+'</label><img id="delbirth'+index+'" onclick="delBirth('+item.id+')" src="img/icons/delete.png" />' +
                        '                    </div>' ;
                }else{
                    strBirthHtml = strBirthHtml+ '<div class="births-item2" id="birth'+index+'" onclick="selectBirth('+item.id+','+index+','+birthNum+')">\n' +
                        '                        <label>'+item.name+'</label><img id="delbirth'+index+'" onclick="delBirth('+item.id+')" src="img/icons/delete.png" />' +
                        '                    </div>' ;
                }

            })
            // $("#births").html(strBirthHtml);
        }
    })

})